package cn.kgc.mes.productreceptline.service.impl;

import java.math.BigDecimal;
import java.util.List;
import cn.kgc.mes.common.utils.DateUtils;
import cn.kgc.mes.storage.domain.MaterialStock;
import cn.kgc.mes.storage.mapper.MaterialStockMapper;
import cn.kgc.mes.storage.service.IMaterialStockService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.kgc.mes.productreceptline.mapper.ProductRecptLineMapper;
import cn.kgc.mes.productreceptline.domain.ProductRecptLine;
import cn.kgc.mes.productreceptline.service.IProductRecptLineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 产品入库记录行Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-21
 */
@Service
public class ProductRecptLineServiceImpl extends ServiceImpl<ProductRecptLineMapper, ProductRecptLine> implements IProductRecptLineService
{
    @Autowired
    private ProductRecptLineMapper productRecptLineMapper;
    //库存service对象
    @Autowired
    private IMaterialStockService materialStockService;
    /**
     * 查询产品入库记录行
     * 
     * @param lineId 产品入库记录行主键
     * @return 产品入库记录行
     */
    @Override
    public ProductRecptLine selectProductRecptLineByLineId(Long lineId)
    {
        return productRecptLineMapper.selectProductRecptLineByLineId(lineId);
    }

    /**
     * 查询产品入库记录行列表
     * 
     * @param productRecptLine 产品入库记录行
     * @return 产品入库记录行
     */
    @Override
    public List<ProductRecptLine> selectProductRecptLineList(ProductRecptLine productRecptLine)
    {
        return productRecptLineMapper.selectProductRecptLineList(productRecptLine);
    }

    /**
     * 新增产品入库记录行
     * 
     * @param productRecptLine 产品入库记录行
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProductRecptLine(ProductRecptLine productRecptLine) {
        int c = 0;
        productRecptLine.setCreateTime(DateUtils.getNowDate());
        //新增产品入库
        int a = productRecptLineMapper.insertProductRecptLine(productRecptLine);
        //修改库存2
        //2.1先查询当前的库存
        LambdaQueryWrapper<MaterialStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialStock::getItemId,productRecptLine.getItemId());
        MaterialStock materialStock = materialStockService.getOne(queryWrapper);
        //当前库存量
        BigDecimal quantityOnhand = materialStock.getQuantityOnhand();
        //2.2当前库存加上新增入库的库存
        Integer newQuantity = quantityOnhand.intValue()+productRecptLine.getQuantityRecived().intValue();
        //更新库存数量
        materialStock.setQuantityOnhand(new BigDecimal(newQuantity));
        boolean b = materialStockService.updateById(materialStock);
        if (b){
            c=1;
        }
        return a+c;
    }

    /**
     * 修改产品入库记录行
     * 
     * @param productRecptLine 产品入库记录行
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProductRecptLine(ProductRecptLine productRecptLine)
    {
        int i = 0;
        productRecptLine.setUpdateTime(DateUtils.getNowDate());
        //先获取修改前的数据
        QueryWrapper<ProductRecptLine> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("line_id", productRecptLine.getLineId());
        ProductRecptLine oldProductRecptLine = productRecptLineMapper.selectOne(queryWrapper);
        if(oldProductRecptLine != null){
            i = productRecptLineMapper.updateProductRecptLine(productRecptLine);
        }
        if (i>0){
            //更新库存数量
            LambdaQueryWrapper<MaterialStock> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(MaterialStock::getItemId,productRecptLine.getItemId());
            //当前库存量对象
            MaterialStock materialStock = materialStockService.getOne(queryWrapper2);
            //当前库存量
            BigDecimal quantityOnhand = materialStock.getQuantityOnhand();
            //修改前行库存
            BigDecimal oldUp = oldProductRecptLine.getQuantityRecived();
            //修改后的行库存
            BigDecimal newUp = productRecptLine.getQuantityRecived();
            //新的库存
            Integer newQuantityOnhand = quantityOnhand.intValue() - oldUp.intValue() + newUp.intValue();
            materialStock.setQuantityOnhand(new BigDecimal(newQuantityOnhand));
            boolean b = materialStockService.updateById(materialStock);
            if (b){
                i=1;
            }
        }
        return i;
    }



    /**
     * 删除产品入库记录行信息
     * 
     * @param lineId 产品入库记录行主键
     * @return 结果
     */
    @Override
    public int deleteProductRecptLineByLineId(Long lineId)
    {
        System.out.println("执行了单行删除");
        int i = 0;
        //获得删除对象的信息
        ProductRecptLine productRecptLine = selectProductRecptLineByLineId(lineId);
        LambdaQueryWrapper<MaterialStock> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MaterialStock::getItemId,productRecptLine.getItemId());
        //获取到当前关联物料的库存对象
        MaterialStock stock = materialStockService.getOne(queryWrapper);
        System.out.println("删除数量"+productRecptLine.getQuantityRecived());
        System.out.println("库存数量1"+stock.getQuantityOnhand());
        Integer quantityOnhand = stock.getQuantityOnhand().intValue()-productRecptLine.getQuantityRecived().intValue();
        System.out.println("库存数量2"+quantityOnhand);
        //更新当前库存数量
        stock.setQuantityOnhand(new BigDecimal(quantityOnhand));
        boolean a = materialStockService.updateById(stock);
        if (a){
            //删除该对象
            i = productRecptLineMapper.deleteProductRecptLineByLineId(lineId);
        }
        return i;
    }
}
